package com.huanletao.reflect;

import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/**
 * <p>
 *     MethodUtils 该类是调用对象方法的工具类。
 *     ConstructorUtils 该类是调用对象的构造方法，生成实例的工具类
 *     ClassUtils 是取得类信息的工具类，例如，包名，类名，所继承的父类，所实现的接口信息等。
 * </p>
 *
 * description: MethodUtilsApi <br>
 * date: 2022/7/1 9:22 <br>
 * author: 踏雪无痕 <br>
 * version: 1.0 <br>
 */
public class MethodUtilsApi {

    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        // MethodUtils.invokeMethod实现方法调用
        System.err.println(MethodUtils.invokeStaticMethod(Integer.class, "max", 1, 2));
    }

    @Test
    public void test() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, InstantiationException {
        // ConstructorUtils类创建的对象
        System.out.println(ConstructorUtils.invokeConstructor(String.class, "iosoft2020"));

    }

    /**
     *  MethodUtils.invokeMethod实现方法调用
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     */
    @Test
    public void test01() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        //调用静态方法
        System.out.println(MethodUtils.invokeStaticMethod(Integer.class, "max", 1, 2));
        String equalsIgnoreCase = "iosoft2020";
        //调用非静态方法
        System.out.println(MethodUtils.invokeMethod(equalsIgnoreCase, "equalsIgnoreCase", "IOSOFT2020"));
    }

    @Test
    public void test3(){
        //获取包名
        String packageName = ClassUtils.getPackageName("com.huanletao.reflect.MethodUtilsApi");
        System.out.println("packageName = " + packageName);
        String packageName1 = ClassUtils.getPackageName(MethodUtilsApi.class);

        System.out.println("packageName1 = " + packageName1);

        //获取类名；
        System.err.println(ClassUtils.getShortClassName(String.class));
        System.err.println(ClassUtils.getShortClassName("java.lang.String"));

        ClassUtils.getAllInterfaces(ArrayList.class).forEach(System.err::println);

        ClassUtils.getAllSuperclasses(ArrayList.class).forEach(System.err::println);
    }
}
